﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;
using YArchitech.Revit.Electric.Common;

namespace Model
{
	public partial class DlgCableTrayCode : System.Windows.Forms.Form
	{
		public DlgCableTrayCode()
		{
			this.InitializeComponent();
		}

		private void DlgCableTrayCode_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.xmlHelper.LoadXml(this.path, "Root");
			this.codeInfo[0] = "CT";
			this.codeInfo[1] = "?";
			this.codeInfo[2] = "?";
			this.codeInfo[3] = "1";
			this.LoadDlgData();
		}

		public void SaveDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYCableTrayCode"));
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYCableTrayCode", "Params"));
			this.xmlHelper.SetAttribute("CableTrayCodeValue", this.txtCableTrayCode.Text);
			this.xmlHelper.SetAttribute("FormatCode", this.chkFormatCode.Checked.ToString());
			this.xmlHelper.SetAttribute("Mark", this.chkMark.Checked.ToString());
			this.xmlHelper.SetAttribute("MarkValue", this.txtMark.Text);
			this.xmlHelper.SetAttribute("Level", this.chkLevel.Checked.ToString());
			this.xmlHelper.SetAttribute("Direction", this.chkDirection.Checked.ToString());
			this.xmlHelper.SetAttribute("DigitalCode", this.chkDigitalCode.Checked.ToString());
			this.xmlHelper.SetAttribute("DigitalCodeValue", this.txtDigitalCode.Text);
			this.xmlHelper.SaveXml("");
		}

		public void LoadDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYCableTrayCode", "Params"));
			string attribute = this.xmlHelper.GetAttribute("CableTrayCodeValue");
			if (!string.IsNullOrEmpty(attribute))
			{
				this.txtCableTrayCode.Text = attribute;
			}
			string attribute2 = this.xmlHelper.GetAttribute("FormatCode");
			if (!string.IsNullOrEmpty(attribute2))
			{
				this.chkFormatCode.Checked = Convert.ToBoolean(attribute2);
			}
			string attribute3 = this.xmlHelper.GetAttribute("Mark");
			if (!string.IsNullOrEmpty(attribute3))
			{
				this.chkMark.Checked = Convert.ToBoolean(attribute3);
			}
			string attribute4 = this.xmlHelper.GetAttribute("MarkValue");
			if (!string.IsNullOrEmpty(attribute4))
			{
				this.txtMark.Text = attribute4;
			}
			string attribute5 = this.xmlHelper.GetAttribute("Level");
			if (!string.IsNullOrEmpty(attribute5))
			{
				this.chkLevel.Checked = Convert.ToBoolean(attribute5);
			}
			string attribute6 = this.xmlHelper.GetAttribute("Direction");
			if (!string.IsNullOrEmpty(attribute6))
			{
				this.chkDirection.Checked = Convert.ToBoolean(attribute6);
			}
			string attribute7 = this.xmlHelper.GetAttribute("DigitalCode");
			if (!string.IsNullOrEmpty(attribute7))
			{
				this.chkDigitalCode.Checked = Convert.ToBoolean(attribute7);
			}
			string attribute8 = this.xmlHelper.GetAttribute("DigitalCodeValue");
			if (!string.IsNullOrEmpty(attribute8))
			{
				this.txtDigitalCode.Text = attribute8;
			}
		}

		private void chkFormatCode_CheckedChanged(object sender, EventArgs e)
		{
			if (!this.chkFormatCode.Checked)
			{
				if (string.IsNullOrEmpty(this.txtCableTrayCode.Text))
				{
					this.txtCableTrayCode.Text = "CT";
				}
				this.txtCableTrayCode.Enabled = true;
				this.txtCableTrayCode.Focus();
				this.txtMark.Enabled = false;
				this.txtDigitalCode.Enabled = false;
				this.chkMark.Enabled = false;
				this.chkLevel.Enabled = false;
				this.chkDirection.Enabled = false;
				this.chkDigitalCode.Enabled = false;
				return;
			}
			this.txtCableTrayCode.Enabled = false;
			this.txtMark.Enabled = true;
			this.txtDigitalCode.Enabled = true;
			this.chkMark.Enabled = true;
			this.chkLevel.Enabled = true;
			this.chkDirection.Enabled = true;
			this.chkDigitalCode.Enabled = true;
			if (!this.chkMark.Checked && !this.chkLevel.Checked && !this.chkDirection.Checked && !this.chkDigitalCode.Checked)
			{
				this.chkMark.Checked = true;
				this.chkLevel.Checked = true;
				this.chkDirection.Checked = true;
				this.chkDigitalCode.Checked = true;
				return;
			}
			this.chkMark_CheckedChanged(this.chkMark, e);
			this.chkLevel_CheckedChanged(this.chkLevel, e);
			this.chkDirection_CheckedChanged(this.chkDirection, e);
			this.chkDigitalCode_CheckedChanged(this.chkDigitalCode, e);
		}

		private void chkMark_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chkFormatCode.Checked)
			{
				if (this.chkMark.Checked)
				{
					this.txtMark.Enabled = true;
					this.codeInfo[0] = this.txtMark.Text;
				}
				else
				{
					this.txtMark.Enabled = false;
					this.codeInfo[0] = "";
				}
				string trayCodeValue = this.GetTrayCodeValue();
				if (!string.IsNullOrEmpty(trayCodeValue))
				{
					this.txtCableTrayCode.Text = trayCodeValue;
					return;
				}
				this.CodeCompoundModeSet(DlgCableTrayCode.CodeCompoundMode.Mark);
			}
		}

		private void chkLevel_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chkFormatCode.Checked)
			{
				if (this.chkLevel.Checked)
				{
					this.codeInfo[1] = "?";
				}
				else
				{
					this.codeInfo[1] = "";
				}
				string trayCodeValue = this.GetTrayCodeValue();
				if (!string.IsNullOrEmpty(trayCodeValue))
				{
					this.txtCableTrayCode.Text = trayCodeValue;
					return;
				}
				this.CodeCompoundModeSet(DlgCableTrayCode.CodeCompoundMode.Level);
			}
		}

		private void chkDirection_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chkFormatCode.Checked)
			{
				if (this.chkDirection.Checked)
				{
					this.codeInfo[2] = "?";
				}
				else
				{
					this.codeInfo[2] = "";
				}
				string trayCodeValue = this.GetTrayCodeValue();
				if (!string.IsNullOrEmpty(trayCodeValue))
				{
					this.txtCableTrayCode.Text = trayCodeValue;
					return;
				}
				this.CodeCompoundModeSet(DlgCableTrayCode.CodeCompoundMode.Direction);
			}
		}

		private void chkDigitalCode_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chkFormatCode.Checked)
			{
				if (this.chkDigitalCode.Checked)
				{
					this.txtDigitalCode.Enabled = true;
					this.codeInfo[3] = this.txtDigitalCode.Text;
				}
				else
				{
					this.txtDigitalCode.Enabled = false;
					this.codeInfo[3] = "";
				}
				string trayCodeValue = this.GetTrayCodeValue();
				if (!string.IsNullOrEmpty(trayCodeValue))
				{
					this.txtCableTrayCode.Text = trayCodeValue;
					return;
				}
				this.CodeCompoundModeSet(DlgCableTrayCode.CodeCompoundMode.DigitalCode);
			}
		}

		private string GetTrayCodeValue()
		{
			string text = "";
			foreach (string text2 in this.codeInfo)
			{
				if (!string.IsNullOrEmpty(text2))
				{
					text = text + text2 + "-";
				}
			}
			if (text.Length > 0)
			{
				text = text.Remove(text.Length - 1, 1);
			}
			else
			{
				TaskDialog.Show("盈建科提示", "桥架编码不能为空,至少选择一项。");
			}
			return text;
		}

		private void CodeCompoundModeSet(DlgCableTrayCode.CodeCompoundMode mode)
		{
			if (mode == DlgCableTrayCode.CodeCompoundMode.Mark)
			{
				this.chkMark.Checked = true;
				this.txtMark.Enabled = true;
				return;
			}
			if (mode == DlgCableTrayCode.CodeCompoundMode.Level)
			{
				this.chkLevel.Checked = true;
				return;
			}
			if (mode == DlgCableTrayCode.CodeCompoundMode.Direction)
			{
				this.chkDirection.Checked = true;
				return;
			}
			if (mode == DlgCableTrayCode.CodeCompoundMode.DigitalCode)
			{
				this.chkDigitalCode.Checked = true;
				this.txtDigitalCode.Enabled = true;
			}
		}

		private void btnDirection_Click(object sender, EventArgs e)
		{
			TaskDialog.Show("桥架方向说明", "桥架方向：X:横向；Y:纵向；Z:竖向；T:其他方向。");
		}

		private void txtMark_TextChanged(object sender, EventArgs e)
		{
			if (this.txtMark.Text.Length == 0)
			{
				TaskDialog.Show("盈建科提示", "参数不能为空。");
				this.txtMark.Text = "CT";
				this.txtMark.SelectAll();
			}
			this.codeInfo[0] = this.txtMark.Text;
			if (this.chkFormatCode.Checked)
			{
				this.txtCableTrayCode.Text = this.GetTrayCodeValue();
			}
		}

		private void txtDigitalCode_TextChanged(object sender, EventArgs e)
		{
			if (this.txtDigitalCode.Text.Length == 0)
			{
				TaskDialog.Show("盈建科提示", "参数不能为空。");
				this.txtDigitalCode.Text = "1";
				this.txtDigitalCode.SelectAll();
			}
			this.codeInfo[3] = this.txtDigitalCode.Text;
			if (this.chkFormatCode.Checked)
			{
				this.txtCableTrayCode.Text = this.GetTrayCodeValue();
			}
		}

		private void txtCableTrayCode_TextChanged(object sender, EventArgs e)
		{
			if (this.txtCableTrayCode.Text.Length == 0)
			{
				TaskDialog.Show("盈建科提示", "参数不能为空。");
				this.txtCableTrayCode.Text = "CT";
				this.txtCableTrayCode.SelectAll();
			}
		}

		private void txtDigitalCode_KeyPress(object sender, KeyPressEventArgs e)
		{
			char keyChar = e.KeyChar;
			if (keyChar != '\b' && (keyChar < '0' || keyChar > '9'))
			{
				e.Handled = true;
			}
		}

		private void DlgCableTrayCode_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SaveDlgData();
			this.SendESC();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void SendESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			DlgCableTrayCode.keybd_event((byte)bVk, 0, dwFlags, 0);
			DlgCableTrayCode.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void DlgCableTrayCode_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				DlgCableTrayCode.SendMessage(WindowHelper.HWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper();

		private string path = Path.Combine(Product.WorkingLocation, "CableTrayCode.xml");

		private const string Root = "Root";

		private const string Node = "HYCableTrayCode";

		private const string ChildNode = "Params";

		private const string CableTrayCodeValue = "CableTrayCodeValue";

		private const string FormatCode = "FormatCode";

		private const string Mark = "Mark";

		private const string MarkValue = "MarkValue";

		private const string Level = "Level";

		private const string Direction = "Direction";

		private const string DigitalCode = "DigitalCode";

		private const string DigitalCodeValue = "DigitalCodeValue";

		private string[] codeInfo = new string[4];

		private enum CodeCompoundMode
		{
			Mark,
			Level,
			Direction,
			DigitalCode
		}
	}
}
